package com.hakim.wfb.schedule.config;

import com.hakim.wfb.schedule.task.*;
import com.hkfs.fundamental.config.FundamentalConfigProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * schedule的配置信息
 */
@Configuration
public class ScheduleConfigration {

    @Autowired
    private ScheduleInfoAction scheduleInfoAction;

    //@Autowired
//    private YuekeCinemaSyncTask yuekeCinemaSyncTask;
//
//    @Autowired
//    private YuekeSchedulesSyncTask yuekeSchedulesSyncTask;
//
//    @Autowired
//    private YuekeHallSeatsSyncTask yuekeHallSeatsSyncTask;
//
//    @Autowired
//    private YuekeSchedulesAreaSyncTask yuekeSchedulesAreaSyncJobTask;
//
//    @Autowired
//    private OrdersTimeOutTask ordersTimeOutTask;
//
//    @Autowired
//    private OrdersStatusTask ordersStatusTask;
//
    @Autowired
    private PrintStatusTask printStatusTask;

//
//    @Autowired
//    private ScheduleFilmStopSellingTask scheduleFilmStopSellingTask;
//
//    @Autowired
//    private WeixinPayOrderStatusSyncTask weixinPayOrderStatusSyncTask;

//    @Autowired
//    private WeixinPayReconciliationTask weixinPayReconciliationTask;

    /**
     * 用于5分钟轮训检查cron修改（基本不需要修改）
     *
     * @return
     */
    @Bean(name = "jobDetail")
    public MethodInvokingJobDetailFactoryBean jobDetail() {
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        methodInvokingJobDetailFactoryBean.setConcurrent(false);
        methodInvokingJobDetailFactoryBean.setTargetObject(scheduleInfoAction);
        methodInvokingJobDetailFactoryBean.setTargetMethod("reScheduleJob");
        return methodInvokingJobDetailFactoryBean;
    }

    /**
     * 用于5分钟轮训检查cron修改（基本不需要修改）
     *
     * @return
     */
    @Bean(name = "cronTrigger")
    public CronTriggerFactoryBean cronTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetail().getObject());
        cronTriggerFactoryBean.setCronExpression(FundamentalConfigProvider.getString("task.hakim.wfb.refresh.default.cron"));
        return cronTriggerFactoryBean;
    }

    /**
     * yuekeCinemaSync 任务
     *
     * @return
     */
//    @Bean(name = "yuekeCinemaSyncJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean yuekeCinemaSyncJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(yuekeCinemaSyncTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }

//    @Bean(name = "yuekeCinemaSyncJobCronTrigger")
//    public CronTriggerFactoryBean yuekeCinemaSyncJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(yuekeCinemaSyncJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * yuekeSchedulesSync 任务
     *
     * @return
     */
//    @Bean(name = "yuekeSchedulesSyncJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean yuekeSchedulesSyncJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(yuekeSchedulesSyncTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }

//    @Bean(name = "yuekeSchedulesSyncJobCronTrigger")
//    public CronTriggerFactoryBean yuekeSchedulesSyncJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(yuekeSchedulesSyncJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * yuekeHallSeatsSync 任务
     *
     * @return
     */
//    @Bean(name = "yuekeHallSeatsSyncJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean yuekeHallSeatsSyncJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(yuekeHallSeatsSyncTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "yuekeHallSeatsSyncJobCronTrigger")
//    public CronTriggerFactoryBean yuekeHallSeatsSyncJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(yuekeHallSeatsSyncJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * yuekeSchedulesAreaSyncJob 任务
     *
     * @return
     */
//    @Bean(name = "yuekeSchedulesAreaSyncJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean yuekeSchedulesAreaSyncJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(yuekeSchedulesAreaSyncJobTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "yuekeSchedulesAreaSyncJobCronTrigger")
//    public CronTriggerFactoryBean yuekeSchedulesAreaSyncJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(yuekeSchedulesAreaSyncJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * yuekeSchedulesAreaSyncJob 任务
     *
     * @return
     */
//    @Bean(name = "ordersTimeOutJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean ordersTimeOutJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(ordersTimeOutTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "ordersTimeOutJobCronTrigger")
//    public CronTriggerFactoryBean ordersTimeOutJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(ordersTimeOutJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * scheduleFilmStopSellingJob 任务
     *
     * @return
     */
//    @Bean(name = "scheduleFilmStopSellingJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean scheduleFilmStopSellingJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(scheduleFilmStopSellingTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "scheduleFilmStopSellingJobCronTrigger")
//    public CronTriggerFactoryBean scheduleFilmStopSellingJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(scheduleFilmStopSellingJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * ordersStatusJob 任务
     *
     * @return
     */
//    @Bean(name = "ordersStatusJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean ordersStatusJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(ordersStatusTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "ordersStatusJobCronTrigger")
//    public CronTriggerFactoryBean ordersStatusJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(ordersStatusJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * printStatusJob 任务
     *
     * @return
     */
    @Bean(name = "printStatusJobCronJobDetail")
    public MethodInvokingJobDetailFactoryBean printStatusJobCronJobDetail() {
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        methodInvokingJobDetailFactoryBean.setConcurrent(false);
        methodInvokingJobDetailFactoryBean.setTargetObject(printStatusTask);
        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
        return methodInvokingJobDetailFactoryBean;
    }

    @Bean(name = "printStatusJobCronTrigger")
    public CronTriggerFactoryBean printStatusJobCronTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(printStatusJobCronJobDetail().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
        return cronTriggerFactoryBean;
    }

    /**
     * WeixinPayOrderStatusSync 任务
     *
     * @return
     */
//    @Bean(name = "weixinPayOrderStatusSyncJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean weixinPayOrderStatusSyncJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(weixinPayOrderStatusSyncTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "weixinPayOrderStatusSyncJobCronTrigger")
//    public CronTriggerFactoryBean weixinPayOrderStatusSyncJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(weixinPayOrderStatusSyncJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * WeixinPayReconciliationJob 任务
     *
     * @return
     */
//    @Bean(name = "weixinPayReconciliationJobCronJobDetail")
//    public MethodInvokingJobDetailFactoryBean weixinPayReconciliationJobCronJobDetail() {
//        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//        methodInvokingJobDetailFactoryBean.setConcurrent(false);
//        methodInvokingJobDetailFactoryBean.setTargetObject(weixinPayReconciliationTask);
//        methodInvokingJobDetailFactoryBean.setTargetMethod("doTask");
//        return methodInvokingJobDetailFactoryBean;
//    }
//
//    @Bean(name = "weixinPayReconciliationJobCronTrigger")
//    public CronTriggerFactoryBean weixinPayReconciliationJobCronTrigger() {
//        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
//        cronTriggerFactoryBean.setJobDetail(weixinPayReconciliationJobCronJobDetail().getObject());
//        cronTriggerFactoryBean.setCronExpression("0 0 10 * * ?");
//        return cronTriggerFactoryBean;
//    }

    /**
     * 调度工厂
     *
     * @return
     */
    @Bean(name = "schedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        //在添加新的触发器时，增加相应的触发器
        schedulerFactoryBean.setTriggers(cronTrigger().getObject(),
                printStatusJobCronTrigger().getObject()
        );
        /**
         ordersStatusJobCronTrigger().getObject()
         */

        return schedulerFactoryBean;
    }

}
